// pages/homepagesearch/index.js
const {
  searchindexlist,
  searchmerlist,
  searchtriballist
} = require('../../utils/api')
const { fetch } = require('../../utils/fetch')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstWord:'',
    merword:'',
    tribalWord:'',
    isTrue:false,
    triplace:'',
    searchType:'',
    mertype:'',
    searchlist:[],
    indexsearchnum:1,
    merchantsList:[],
    tribetopiclist:[],
    specificarea:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }

      this.setData({
        searchType:options.searchpage,
        triplace:options.type,
        mertype:options.type
      })
      switch (options.type || options.searchpage) {
        case 'index':
          wx.setNavigationBarTitle({
            title: '首页搜索',
          })
          break
        case 'merchants':
          wx.setNavigationBarTitle({
            title: '商户搜索',
          })
          break
        case '搜索话题':
          wx.setNavigationBarTitle({
            title: '搜索话题',
          })
          break
        // case '搜索部落':
        //   wx.setNavigationBarTitle({
        //     title: '搜索部落',
        //   })
        //   break
        default:
          wx.setNavigationBarTitle({
            title: '小微服务',
          })
          break
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  
    // 搜索
    goSearch: function(e) {
      var that = this;
    
      if(e.detail.value){
        this.setData({
          firstWord: e.detail.value,
          isTrue:true
        })
      } else{
              wx.showToast({
                  title: '输入不能为空',
                  icon: 'none',
                  duration: 1500
              })
      }
   
        
      },
      searchEnter:function(e){
        fetch(`wx/message/${this.data.specificarea}/search`, 'POST', {
          keyword:this.data.firstWord
        }).then(res => {
            if(res){
              this.setData({
                searchlist:res.list
              })
            }else {
              wx.showToast({
                title: '暂无搜索数据',
                icon: 'none',
                duration: 1500
            })
            }
        })
      },
      // 按钮搜索
      goFind:function(){
        fetch(`wx/message/${this.data.specificarea}/search`, 'POST', {
          keyword:this.data.firstWord
        }).then(res => {
            if(res){
              this.setData({
                searchlist:res.list
              })
            }  else {
              wx.showToast({
                title: '暂无搜索数据',
                icon: 'none',
                duration: 1500
            })
            }  
        })
      },
      goClose:function(){
        this.setData({
          firstWord:'',
          isTrue:false
        })
      },
// 商户搜索
      gomerSearch:function(e){
        if(e.detail.value){
          this.setData({
            merword: e.detail.value,
            isTrue:true
          })
        } else{
                wx.showToast({
                    title: '输入不能为空',
                    icon: 'none',
                    duration: 1500
                })
        }
      },
    
      merEnter:function(){
        var that = this;
        fetch(`wx/store/${this.data.specificarea}/search`, 'POST', {
          keyword:this.data.merword,
        }).then(res => {
          if(res) {
            that.setData({
              merchantsList:res.list
            })
          }else {
            wx.showToast({
              title: '暂无搜索数据',
              icon: 'none',
              duration: 1500
          })
          }
        })
      },
      gomerFind:function(){
        var that = this;
        fetch(`wx/store/${this.data.specificarea}/search`, 'POST', {
          keyword:this.data.merword
        }).then(res => {
          if(res) {
            that.setData({
              merchantsList:res.list
            })
          }else {
            wx.showToast({
              title: '暂无搜索数据',
              icon: 'none',
              duration: 1500
          })
          }
        })
      },
        // 部落、话题搜索
      gotribalSearch:function(e){
        if(e.detail.value){
          this.setData({
            tribalWord: e.detail.value,
            isTrue:true
          })
        } else{
            wx.showToast({
                title: '输入不能为空',
                icon: 'none',
                duration: 1500
            })
        }
      },

      tribalEnter:function(){
        var that = this;
        // if(this.data.mertype =='搜索话题') {
          fetch(`wx/tribe/${this.data.specificarea}/search`, 'POST', {
            keyword:this.data.tribalWord,
            // type:2
          }).then(res => {
            if(res) {
              res.list.map(val=>{
                val.photohead = val.photohead ? val.photohead.split(',') : val.photohead
            })
              that.setData({
                tribetopiclist:res.list
              })
            }else {
              wx.showToast({
                title: '暂无搜索数据',
                icon: 'none',
                duration: 1500
            })
            }
          })
        // }
      },
      gotribalFind:function(){
        var that = this;
        if(this.data.mertype =='搜索话题') {
          fetch(`wx/tribe/${this.data.specificarea}/search`, 'POST', {
            keyword:this.data.tribalWord,
            type:2
          }).then(res => {
            if(res) {
              res.list.map(val=>{
                val.photohead = val.photohead ? val.photohead.split(',') : val.photohead
            })
            console.log(res.list, '思路打开房间路上看到房价')
              that.setData({
                tribetopiclist:res.list
              })
            }else {
              wx.showToast({
                title: '暂无搜索数据',
                icon: 'none',
                duration: 1500
            })
            }
          })
        }

      },

       // 话题详情
    topicDetails(e) {
      let { id } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/tongdetail/index?id=${id}`,
     })
      // wx.navigateTo({
      //     url: `/pages/topicDetails/index?id=${id}`
      // })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})